package lock

import (
	"sync"
	"sync/atomic"
)

const (
	locked   = 1
	unlocked = 0
)

// 状态锁: 支持查询锁的状态
type statusLock struct {
	mu     sync.Mutex
	status uint32
}

func NewStatusLock() *statusLock {
	return &statusLock{}
}

func (s *statusLock) Lock() {
	s.mu.Lock()
	atomic.StoreUint32(&s.status, locked)
}

func (s *statusLock) Unlock() {
	atomic.StoreUint32(&s.status, unlocked)
	s.mu.Unlock()
}

func (s *statusLock) IsLocked() bool {
	return atomic.LoadUint32(&s.status) == locked
}
